﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    /// <summary>
    /// 多图上传的表单字段
    /// </summary>
    public class PageForm_MultiImage:PageForm_FieldBase
    {
        private List<string> _imageUrls=new List<string>();
        private bool _enableUpload = true;
        private string _ajaxUploadUrl = "";
        private string _width = "80px";
        private string _height = "80px";
        private string _uploadScript = "";

        public PageForm_MultiImage(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-cells_multiimageuploader");
        }

        public PageForm_MultiImage ImageUrls(IEnumerable<string> urls)
        {
            _imageUrls.AddRange(urls);

            return this;
        }

        public PageForm_MultiImage Ajax(string postUrl)
        {
            _ajaxUploadUrl = postUrl;

            return this;
        }

        ///// <summary>
        ///// 默认图片框的高度
        ///// </summary>
        ///// <param name="width"></param>
        ///// <param name="height">如果高度为空,则自动等于宽度</param>
        ///// <returns></returns>
        //public PageForm_MultiImage ImageSize(string width, string height = "")
        //{
        //    _width = width;
        //    _height = height;

        //    return this;
        //}

        /// <summary>
        /// 自定义上传图片的脚本代码,点击上传的时候,会调用该脚本,如果为空,则使用自定义的脚本
        /// </summary>
        /// <param name="uploadscript"></param>
        /// <returns></returns>
        public PageForm_MultiImage UploadScript(string uploadscript)
        {
            _uploadScript = uploadscript.ToStringEx();

            return this;
        }


        public PageForm_MultiImage DisableUpload(bool disableUpload = true)
        {
            _enableUpload = !disableUpload;

            return this;
        }

        protected override void RenderHeader()
        {
            //base.RenderHeader();
        }

        protected override void BeginRender()
        {
            base.BeginRender();

            //this.Data("enableupload", _enableUpload.ToStringEx());

            if (_enableUpload)
            {
                this.Data("data-enableupload", _enableUpload.ToStringEx());

                this.Data("name", getName());

                //if (_postUrl=="")
                //{
                //    throw new ArgumentOutOfRangeException("AjaxUploadUrl");
                //}
                if (!string.IsNullOrWhiteSpace(_ajaxUploadUrl))
                {

                    this.Data("data-ajaxposturl", _ajaxUploadUrl);
                }

            }

            if (!string.IsNullOrWhiteSpace(_uploadScript))
            {
                this.Data("data-uploadscript", _uploadScript);
            }
        }

        protected override void RenderBody()
        {
            /*
             <div class="weui-uploader">
                <div class="weui-uploader__hd">
                  <p class="weui-uploader__title">图片上传</p>
                  <div class="weui-uploader__info">0/2</div>
                </div>
                <div class="weui-uploader__bd">
                  <ul class="weui-uploader__files" id="uploaderFiles">
                    <li class="weui-uploader__file" style="background-image:url(./images/pic_160.png)"></li>
                    <li class="weui-uploader__file weui-uploader__file_status" style="background-image:url(./images/pic_160.png)">
                      <div class="weui-uploader__file-content">
                        <i class="weui-icon-warn"></i>
                      </div>
                    </li>
                    <li class="weui-uploader__file weui-uploader__file_status" style="background-image:url(./images/pic_160.png)">
                      <div class="weui-uploader__file-content">50%</div>
                    </li>
                  </ul>
                  <div class="weui-uploader__input-box">
                    <input id="uploaderInput" class="weui-uploader__input" type="file" accept="image/*" multiple="">
                  </div>
                </div>
              </div>
             */

            ViewContext.Writer.WriteLine("<div class=\"weui-uploader\">");

            if (!string.IsNullOrWhiteSpace(getLabel()))
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-uploader__hd\">");
                ViewContext.Writer.WriteLine($"<p class=\"weui-uploader__title\">{getLabel()}</p>");
                ViewContext.Writer.WriteLine("</div> <!-- end  uploader__hd-->");
            }
            
            ViewContext.Writer.WriteLine("<div class=\"weui-uploader__bd\">");

            ViewContext.Writer.WriteLine("<ul class=\"weui-uploader__files\">");

            foreach (var imageUrl in _imageUrls)
            {
                ViewContext.Writer.WriteLine($"<li class=\"weui-uploader__file\">");

                ViewContext.Writer.WriteLine($"<img src=\"{imageUrl}\" />");

                ViewContext.Writer.WriteLine($"<input type=\"hidden\" name=\"{getName()}\" value=\"{imageUrl}\"/>");

                ViewContext.Writer.WriteLine("</li>");
            }
            
            ViewContext.Writer.WriteLine("</ul> <!-- end uploader__files -->");

            if (_enableUpload)
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-uploader__input-box\">");

                ViewContext.Writer.WriteLine("</div> <!-- end  uploader__input-box -->");
            }

            ViewContext.Writer.WriteLine("</div> <!-- end uploader__bd -->");

            ViewContext.Writer.WriteLine("</div> <!-- end weui-uploader -->");
        }
    }
}